package com.softgroup.breath.easyecgproject.utils;

import android.util.Log;

import com.softgroup.breath.easyecgproject.entry.CommandInfo;
import com.softgroup.breath.easyecgproject.myapi.EcgCount;
import com.softgroup.breath.easyecgproject.myapi.HeartRateCount;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;

import static com.softgroup.breath.easyecgproject.myapi.EcgCount.getV;
import static com.softgroup.breath.easyecgproject.myapi.FormatCast.bytesToHexString;

/**
 * Created by Administrator on 2017/9/25.
 */

public class DataManager {

    private static final byte[] HUNDRED_MODE = {0x30, 0x30};
    private static final byte[] DOUBLE_HUNDRED_MODE = {0x30, 0x31};
    private static final byte[] THOUSAND_MODE = {0x30, 0x32};
    private static final byte[] DOUBLE_THOUSAND_MODE = {0x30, 0x33};
    private static final byte[] HUNDRED_DISTANCE_MODE = {0x30, 0x30};
    private static final byte[] DOUBLE_HUNDRED_DISTANCE_MODE = {0x30, 0x31};
    private static DataManager dataManager;
    private AnswerCallback answerCallBack;
    private byte[] dataPageOne;
    private byte[] dataPageTwo;
    private byte[] sendDataPageOne;
    private byte[] sendDataPageTwo;
    private int testMode = 0;

    public int getPageNumber() {
        return pageNumber;
    }

    private int pageNumber = 0;
    private int nowPageNumber = 0;
    private long baseNumber;
    private int subscript;
    private int pageSize = 1200;
    private int cacheSize = 2400;
    private int[] ECGData;
    private float[] ECGDataHighPassed;
    private long lastDate;
    private String userId = "ee42015e7e2441ccac";

    public long getBaseNumber() {
        return baseNumber;
    }

    private boolean isWaiting;

    public void setUserBond(boolean userBond) {
        isUserBond = userBond;
    }

    private boolean isUserBond;

    public boolean isTransport() {
        return isTransport;
    }


    private boolean isTransport;
    private boolean isOneFull;


    private boolean isKnow;

    private int oldSub = 0;
    private int subAdd = 0;
    private byte[] bytes1 = new byte[20];
    private int power = 100;
    private long sumData = 0;
    private int sub;

    public int getByteNumber() {
        return byteNumber;
    }

    private int byteNumber;

    public String getDeviceId() {
        return deviceId;
    }

    private String deviceId = "88:88:88:88:88:88";
    private String deviceGatherState = "未知";
    private String gatherDate;
    private final float HIGH_PASS_ARGUMENT = 0.97f;
    private final float LOW_PASS_ARGUMENT = 0.005f;
    private final float HIGH_PASS_ARGUMENT_DRAW = 0.998f;
    private boolean isFinish = true;
    private int pageTitleNumber = 156;
    private int kilometer;
    private float[] firstChannel;
    private float[] secondChannel;
    private float[] thirdChannel;
    private float[] fourthChannel;
    private float[] fifthChannel;
    private float[] sixthChannel;
    private int channelMode = 1;
    private int subscriptHalf;
    private float[] ecgDataLowPass;
    private float smoothSum;

    private DataManager() {

    }


    public void initDataManager(int mode, int time) {
        isOneFull = false;
        if (mode % 2 == 0) {
            dataPageOne = new byte[3000];
            dataPageTwo = new byte[3000];
            sendDataPageOne = new byte[3000 + pageTitleNumber];
            sendDataPageTwo = new byte[3000 + pageTitleNumber];
        } else {
            dataPageOne = new byte[6000];
            dataPageTwo = new byte[6000];
            sendDataPageOne = new byte[6000 + pageTitleNumber];
            sendDataPageTwo = new byte[6000 + pageTitleNumber];
        }
        switch (mode) {
            case 0:
                sendDataPageOne[1] = HUNDRED_MODE[1];
                sendDataPageTwo[1] = HUNDRED_MODE[1];
                pageNumber = time;
                break;
            case 1:
                sendDataPageOne[0] = DOUBLE_HUNDRED_MODE[0];
                sendDataPageTwo[0] = DOUBLE_HUNDRED_MODE[0];
                sendDataPageOne[1] = DOUBLE_HUNDRED_MODE[1];
                sendDataPageTwo[1] = DOUBLE_HUNDRED_MODE[1];
                pageNumber = time;
                break;
            case 2:
                sendDataPageOne[1] = THOUSAND_MODE[1];
                sendDataPageTwo[1] = THOUSAND_MODE[1];
                pageNumber = time * 10;
                break;
            case 3:
                sendDataPageOne[1] = DOUBLE_THOUSAND_MODE[1];
                sendDataPageTwo[1] = DOUBLE_THOUSAND_MODE[1];
                pageNumber = time * 10;
                break;
            case 4:
                sendDataPageOne[1] = HUNDRED_DISTANCE_MODE[1];
                sendDataPageTwo[1] = HUNDRED_DISTANCE_MODE[1];
                pageNumber = time;
                break;
            case 5:
                sendDataPageOne[1] = DOUBLE_HUNDRED_DISTANCE_MODE[1];
                sendDataPageTwo[1] = DOUBLE_HUNDRED_DISTANCE_MODE[1];
                pageNumber = time;
                break;
        }
//        historyData=new int[time*1000];
//        historyByte = new byte[time * 3000];
        initSendDataPage();
        ECGData = new int[cacheSize];
        firstChannel = new float[cacheSize];
        secondChannel = new float[cacheSize];
        thirdChannel = new float[cacheSize];
        fourthChannel = new float[cacheSize];
        fifthChannel = new float[cacheSize];
        sixthChannel = new float[cacheSize];
        ECGDataHighPassed = new float[cacheSize];
        ecgDataLowPass = new float[cacheSize];
        isWaiting = false;
        isOneFull = false;
        baseNumber = 8388608;
        nowPageNumber = 0;
        byteNumber = 0;
        sumData = 0;
        subscript = 0;
        subscriptHalf = 0;
        setGatherDate();
    }

    private void setBaseNumber(int data, int sub) {
        if (sub < 200) {
            sumData += data;
            baseNumber = sumData / (sub + 1);
        } else {
            sumData += data - ECGData[(sub - 200) % cacheSize];
            baseNumber = sumData / 200;
        }

    }

    private void initSendDataPage() {
        for (int i = 2; i < 106; i++) {
            if (i < 20) {
                sendDataPageOne[i] = (byte) userId.charAt(i - 2);
                sendDataPageTwo[i] = (byte) userId.charAt(i - 2);
            } else if (i < 44) {
                if (i - 20 < deviceId.length()) {
                    sendDataPageOne[i] = (byte) deviceId.charAt(i - 20);
                    sendDataPageTwo[i] = (byte) deviceId.charAt(i - 20);
                } else {
                    sendDataPageOne[i] = 0x30;
                    sendDataPageTwo[i] = 0x30;
                }
            } else if (i < 58) {
                sendDataPageOne[i] = 0x43;
                sendDataPageTwo[i] = 0x43;
            } else if (i < 74) {
                sendDataPageOne[i] = 0x44;
                sendDataPageTwo[i] = 0x44;
            } else if (i < 82) {
                sendDataPageOne[i] = 0x30;
                sendDataPageTwo[i] = 0x30;
            } else if (i < 98) {
                sendDataPageOne[i] = 0x31;
                sendDataPageTwo[i] = 0x31;
            } else if (i < 102) {
                if (i == 101) {
                    sendDataPageOne[i] = 0x30;
                    sendDataPageTwo[i] = 0x30;
                } else {
                    sendDataPageOne[i] = 0x30;
                    sendDataPageTwo[i] = 0x30;
                }
            } else if (i < 106) {
                if (i == 105) {
                    sendDataPageOne[i] = 0x30;
                    sendDataPageTwo[i] = 0x30;
                } else {
                    sendDataPageOne[i] = 0x30;
                    sendDataPageTwo[i] = 0x30;
                }
            }
        }
    }

    public static DataManager getInstance() {
        if (dataManager == null) {
            synchronized (DataManager.class) {
                if (dataManager == null) {
                    dataManager = new DataManager();
                }
            }
        }
        return dataManager;
    }

    public void setUserID(String userID) {
        String s = bytesToHexString(userID.getBytes());
        for (int i = 0; i < s.length(); i++) {
            sendDataPageOne[i + 2] = (byte) s.charAt(i);
            sendDataPageTwo[i + 2] = (byte) s.charAt(i);
        }
        this.userId = userID;
    }

    public String getUserId() {
        return userId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
        for (int i = 0; i < deviceId.length(); i++) {
            if (deviceId.charAt(i) != ':') {
                sendDataPageOne[i + 20] = (byte) deviceId.charAt(i);
                sendDataPageTwo[i + 20] = (byte) deviceId.charAt(i);
            }
        }
    }

    public void setGatherDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        Date currentDate = new Date(System.currentTimeMillis());
        gatherDate = format.format(currentDate);
        for (int i = 0; i < gatherDate.length(); i++) {
            sendDataPageOne[i + 44] = (byte) gatherDate.charAt(i);
            sendDataPageTwo[i + 44] = (byte) gatherDate.charAt(i);
        }
    }

    public void setHrs(byte[] hrs) {
        for (int i = 0; i < hrs.length; i++) {
            sendDataPageOne[i + 106] = hrs[i];
            sendDataPageTwo[i + 106] = hrs[i];
        }
    }

    public void setClientID(String clientNumber) {
        int j = 58;
        for (int i = 0; i < clientNumber.length(); i++) {
            if (clientNumber.charAt(i) != ':') {
                sendDataPageOne[j] = (byte) clientNumber.charAt(i);
                sendDataPageTwo[j] = (byte) clientNumber.charAt(i);
                j++;
            }
        }
    }

    public void setTextSize(int textSize) {

    }

    public void setMd5() {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            if (isOneFull) {
                byte[] bytes = md5.digest(dataPageOne);
                for (int i = 0; i < 16; i++) {
                    sendDataPageOne[i + 82] = bytes[i];
                }
            } else {
                byte[] bytes = md5.digest(dataPageTwo);
                for (int i = 0; i < 16; i++) {
                    sendDataPageTwo[i + 82] = bytes[i];
                }
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    private void setPageNumber(int pageNumber) {
        String s = pageNumber + "";
        while (s.length() < 4) {
            s = 0 + s;
        }
        for (int i = 0; i < 4; i++) {
            sendDataPageOne[i + 98] = (byte) s.charAt(i);
            sendDataPageTwo[i + 98] = (byte) s.charAt(i);
        }
    }

    private void setNowPageNumber(int nowPageNumber) {
        this.nowPageNumber = nowPageNumber;
        String s = this.nowPageNumber + "";
        while (s.length() < 4) {
            s = 0 + s;
        }
        for (int i = 0; i < 4; i++) {
            sendDataPageOne[i + 102] = (byte) s.charAt(i);
            sendDataPageTwo[i + 102] = (byte) s.charAt(i);
        }
    }

    private void setDataPage(byte b) {
        if (!isOneFull) {
            dataPageOne[byteNumber % dataPageOne.length] = b;
            sendDataPageOne[byteNumber % dataPageOne.length + pageTitleNumber] = b;
        } else {
            dataPageTwo[byteNumber % dataPageOne.length] = b;
            sendDataPageTwo[byteNumber % dataPageOne.length + pageTitleNumber] = b;
        }
        if (byteNumber % dataPageOne.length == dataPageOne.length - 1) {
            isOneFull = !isOneFull;
        }
        byteNumber++;
    }

    public void writeDataManager(byte[] bytes) {
        int ecg = 0;
        try {
            if (((bytes[0] & 0xff) == 0xfd) && ((bytes[1] & 0xff) == 0xfe)) {
                if (bytes.length == 20) {
                    for (int i = 2; i < 20; i = i + 3) {
                        ecg = ((bytes[i] & 0xFF) << 16) | ((bytes[i + 1] & 0xFF) << 8) | ((bytes[i + 2] & 0xFF));
                        if (channelMode == 1) {
                            if (subscript % 2 == 0) {
                                if (subscriptHalf == 0) {
                                    ECGDataHighPassed[0] = getV(ecg);
                                    firstChannel[0] = getV(ecg);
                                    ecgDataLowPass[0] = getV(ecg);
                                    smoothSum += ecgDataLowPass[0];
                                } else {
                                    ECGDataHighPassed[subscriptHalf % cacheSize] = HIGH_PASS_ARGUMENT * ECGDataHighPassed[(subscriptHalf - 1) % cacheSize] +
                                            HIGH_PASS_ARGUMENT * (getV(ecg) - getV(ECGData[(subscript - 2) % cacheSize]));
                                    firstChannel[subscriptHalf % cacheSize] = HIGH_PASS_ARGUMENT * firstChannel[(subscriptHalf - 1) % cacheSize] +
                                            HIGH_PASS_ARGUMENT * (getV(ecg) - getV(ECGData[(subscript - 2) % cacheSize]));
                                    ecgDataLowPass[subscriptHalf % cacheSize] = LOW_PASS_ARGUMENT * getV(ecg)
                                            + (1 - LOW_PASS_ARGUMENT) * ecgDataLowPass[(subscriptHalf - 1) % cacheSize];
                                    if (subscriptHalf < 20) {
                                        smoothSum += ecgDataLowPass[subscriptHalf % cacheSize];
                                        ecgDataLowPass[subscriptHalf % cacheSize] = smoothSum / subscriptHalf + 1;
                                    } else {
                                        smoothSum -= ecgDataLowPass[(subscriptHalf - 20) % cacheSize];
                                        smoothSum += ecgDataLowPass[subscriptHalf % cacheSize];
                                        ecgDataLowPass[subscriptHalf % cacheSize] = smoothSum / 20;
                                    }
                                }
                            } else {
                                if (subscriptHalf == 0) {
                                    secondChannel[0] = getV(ecg);
                                } else {
                                    secondChannel[subscriptHalf % cacheSize] = HIGH_PASS_ARGUMENT * secondChannel[(subscriptHalf - 1) % cacheSize] +
                                            HIGH_PASS_ARGUMENT * (getV(ecg) - getV(ECGData[(subscript - 2) % cacheSize]));
                                }
                                thirdChannel[subscriptHalf % cacheSize] = secondChannel[subscriptHalf % cacheSize] - firstChannel[subscriptHalf % cacheSize];
                                fourthChannel[subscriptHalf % cacheSize] = -(secondChannel[subscriptHalf % cacheSize] + firstChannel[subscriptHalf % cacheSize]) / 2;
                                fifthChannel[subscriptHalf % cacheSize] = firstChannel[subscriptHalf % cacheSize] - secondChannel[subscriptHalf % cacheSize] / 2;
                                sixthChannel[subscriptHalf % cacheSize] = secondChannel[subscriptHalf % cacheSize] - firstChannel[subscriptHalf % cacheSize] / 2;
                                subscriptHalf++;
                            }
                        } else {
                            if (subscript == 0) {
                                ECGDataHighPassed[0] = getV(ecg);
                            } else {
                                ECGDataHighPassed[subscript % cacheSize] = HIGH_PASS_ARGUMENT * ECGDataHighPassed[(subscript - 1) % cacheSize] +
                                        HIGH_PASS_ARGUMENT * (getV(ecg) - getV(ECGData[(subscript - 1) % cacheSize]));
                            }
                        }
                        if (subscript != 0 & (subscript * 3 + (i - 2) % 3) % (3000 * (channelMode + 1)) == 0) {
                            if (answerCallBack != null) {
                                answerCallBack.onDataPageCallBack();
                                if (channelMode == 0) {
                                    if (pageNumber == nowPageNumber) {
                                        answerCallBack.onPageIsMeet();
                                    }
                                } else {
                                    if (pageNumber * 2 == nowPageNumber) {
                                        answerCallBack.onPageIsMeet();
                                    }
                                }
                            }
                        }
                        for (int j = 0; j < 3; j++) {
                            setDataPage(bytes[i + j]);
                        }
                        ECGData[subscript % cacheSize] = ecg;
                        setBaseNumber(ECGData[subscript % cacheSize], subscript);
                        subscript++;

                    }
                } else {
                    for (int i = 0; i < bytes.length; i++) {
                        bytes1[i] = bytes[i];
                    }
                    sub = bytes.length;
                    isWaiting = true;
                }
            } else {
                if (isWaiting) {
                    for (int i = sub; i < 20; i++) {
                        bytes1[i] = bytes[i - sub];
                    }
                    for (int i = 2; i < 20; i = i + 3) {
                        ecg = ((bytes[i] & 0xFF) << 16) | ((bytes[i + 1] & 0xFF) << 8) | ((bytes[i + 2] & 0xFF));
                        if (channelMode == 1) {
                            if (subscript % 2 == 0) {
                                if (subscriptHalf == 0) {
                                    ECGDataHighPassed[0] = getV(ecg);
                                    firstChannel[0] = getV(ecg);
                                    ecgDataLowPass[0] = getV(ecg);
                                    smoothSum += ecgDataLowPass[0];
                                } else {
                                    ECGDataHighPassed[subscriptHalf % cacheSize] = HIGH_PASS_ARGUMENT * ECGDataHighPassed[(subscriptHalf - 1) % cacheSize] +
                                            HIGH_PASS_ARGUMENT * (getV(ecg) - getV(ECGData[(subscript - 2) % cacheSize]));
                                    firstChannel[subscriptHalf % cacheSize] = HIGH_PASS_ARGUMENT * firstChannel[(subscriptHalf - 1) % cacheSize] +
                                            HIGH_PASS_ARGUMENT * (getV(ecg) - getV(ECGData[(subscript - 2) % cacheSize]));
                                    ecgDataLowPass[subscriptHalf % cacheSize] = LOW_PASS_ARGUMENT * getV(ecg)
                                            + (1 - LOW_PASS_ARGUMENT) * ecgDataLowPass[(subscriptHalf - 1) % cacheSize];
                                    if (subscriptHalf < 20) {
                                        smoothSum += ecgDataLowPass[subscriptHalf % cacheSize];
                                        ecgDataLowPass[subscriptHalf % cacheSize] = smoothSum / subscriptHalf + 1;
                                    } else {
                                        smoothSum -= ecgDataLowPass[(subscriptHalf - 20) % cacheSize];
                                        smoothSum += ecgDataLowPass[subscriptHalf % cacheSize];
                                        ecgDataLowPass[subscriptHalf % cacheSize] = smoothSum / 20 + 1;
                                    }
                                }
                            } else {
                                if (subscriptHalf == 0) {
                                    secondChannel[0] = getV(ecg);
                                } else {
                                    secondChannel[subscriptHalf % cacheSize] = HIGH_PASS_ARGUMENT * secondChannel[(subscriptHalf - 1) % cacheSize] +
                                            HIGH_PASS_ARGUMENT * (getV(ecg) - getV(ECGData[(subscript - 2) % cacheSize]));
                                }
                                thirdChannel[subscriptHalf % cacheSize] = secondChannel[subscriptHalf % cacheSize] - firstChannel[subscriptHalf % cacheSize];
                                fourthChannel[subscriptHalf % cacheSize] = -(secondChannel[subscriptHalf % cacheSize] + firstChannel[subscriptHalf % cacheSize]) / 2;
                                fifthChannel[subscriptHalf % cacheSize] = firstChannel[subscriptHalf % cacheSize] - secondChannel[subscriptHalf % cacheSize] / 2;
                                sixthChannel[subscriptHalf % cacheSize] = secondChannel[subscriptHalf % cacheSize] - firstChannel[subscriptHalf % cacheSize] / 2;
                                subscriptHalf++;
                            }
                        } else {
                            if (subscript == 0) {
                                ECGDataHighPassed[0] = getV(ecg);
                            } else {
                                ECGDataHighPassed[subscript % cacheSize] = HIGH_PASS_ARGUMENT * ECGDataHighPassed[(subscript - 1) % cacheSize] +
                                        HIGH_PASS_ARGUMENT * (getV(ecg) - getV(ECGData[(subscript - 1) % cacheSize]));
                            }
                        }
                        if (subscript != 0 & (subscript * 3 + (i - 2) % 3) % (3000 * (channelMode + 1)) == 0) {
                            if (answerCallBack != null) {
                                answerCallBack.onDataPageCallBack();
                                if (channelMode == 0) {
                                    if (pageNumber == nowPageNumber) {
                                        answerCallBack.onPageIsMeet();
                                    }
                                } else {
                                    if (pageNumber * 2 == nowPageNumber) {
                                        answerCallBack.onPageIsMeet();
                                    }
                                }
                            }
                        }
                        for (int j = 0; j < 3; j++) {
                            setDataPage(bytes1[i + j]);
                        }
                        ECGData[subscript % cacheSize] = ecg;
                        setBaseNumber(ECGData[subscript % cacheSize], subscript);
                        subscript++;
                    }
                    isWaiting = false;
                } else {
                    String s = CommandInfo.getRealAnswer(bytes);
                    Log.e("device_results", s);
                    deviceState(s);
                }
            }
        } catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
        }
    }

    private void deviceState(String s) {
        if (s.contains("设备状态")) {
            deviceGatherState = s.substring(s.indexOf("设备状态") + 5, s.indexOf("设备状态") + 7);
            power = EcgCount.getPower(Integer.valueOf(s.substring(s.indexOf("电量") + 3, s.indexOf("电量") + 8)));
//            if (power < 15) {
//                if (answerCallBack != null) {
//                    answerCallBack.onPowerLow();
//                }
//            }
            isKnow = true;
            if (deviceGatherState.equals("待机")) {
                if (answerCallBack != null) {
                    if (!isUserBond) {
                        answerCallBack.onInit();
                        isUserBond = true;
                    }
                }
            } else if (deviceGatherState.equals("就绪")) {
                Log.e("device_results", "init");
                if (answerCallBack != null) {
                    answerCallBack.onIsGathering();
                }
            }
        } else if (s.contains("停止实时传输执行状态:成功")) {
            isTransport = false;
            setDeviceGatherState("就绪");
        } else if (s.contains("装订用户ID执行状态:成功")) {
            if (answerCallBack != null) {
                answerCallBack.onUserBond(true);
            }
        } else {
            for (String quest : CommandInfo.getArrayQuestName()) {
                if (s.contains(quest)) {
                    if (answerCallBack != null) {
                        answerCallBack.onAnswer(s);
                    }
                }
            }
            if (s.contains(CommandInfo.getArrayQuestName()[9]) || s.contains(CommandInfo.getArrayQuestName()[10])) {
                if (answerCallBack != null) {
                    setDeviceGatherState("未知");
                    answerCallBack.onDeviceStateChange();
                }
            }
        }
    }


    public byte[] getSendDataPage() {
        setNowPageNumber(byteNumber / dataPageOne.length);
        setPageNumber(pageNumber);
        setMd5();
        if (isOneFull) {
            return sendDataPageOne;
        } else {
            return sendDataPageTwo;
        }
    }

    public boolean isUserBond() {
        return isUserBond;
    }


    public String getKilometer() {
        String s = kilometer + "";
        while (s.length() < 2) {
            s = "0" + s;
        }
        return s;
    }

    public void setKilometer(int kilometer) {
        this.kilometer = kilometer;
    }

    public int getTestMode() {
        return testMode;
    }

    public void setGps(double longitude, double latitude) {
        long a = (long) (longitude * 10000000);
        long b = (long) (latitude * 10000000);
        for (int i = 0; i < 8; i++) {
            sendDataPageOne[116 + i] = (byte) ((a >> 8 * i) & 0xff);
            sendDataPageOne[124 + i] = (byte) ((b >> 8 * i) & 0xff);
            sendDataPageTwo[116 + i] = (byte) ((a >> 8 * i) & 0xff);
            sendDataPageTwo[124 + i] = (byte) ((b >> 8 * i) & 0xff);
        }
    }

    public interface AnswerCallback {
        void onAnswer(String s);

        void onPageIsMeet();

        void onDataPageCallBack();

        void onPowerLow();

        void onDeviceStateChange();

        void onUserBond(boolean isSuccess);

        void onBreakTransport();

        void onInit();

        void onIsGathering();

        void onIsTransport();
    }

    public float[] getHeartCountArray() {
        float array[] = null;
        int n = subscript;
        transportState();
        if (n > 0 & n < cacheSize) {
            array = new float[n + 1];
            for (int i = 0; i <= n; i++) {
                array[i] = ECGDataHighPassed[i];
            }
        } else if (n >= cacheSize) {
            array = new float[cacheSize];
            for (int i = 0; i < cacheSize; i++) {
                array[i] = ECGDataHighPassed[(n - cacheSize + i) % cacheSize];
            }
        }
        return array;
    }

    public int[] getDrawArray() {
        int array[] = null;
        int n = subscript;
        transportState();
        if (n > 0 & n < pageSize) {
            array = new int[n + 1];
            for (int i = 0; i <= n; i++) {
                array[i] = ECGData[i];
            }
        } else if (n >= pageSize) {
            array = new int[pageSize];
            for (int i = 0; i < pageSize; i++) {
                array[i] = ECGData[(n - pageSize + i) % cacheSize];
            }
        }
        return array;
    }

    public float[] getDrawArrayH() {
        float array[] = null;
        int n = subscript;
        transportState();
        if (n > 0 & n < pageSize) {
            array = new float[n + 1];
            for (int i = 0; i <= n; i++) {
                array[i] = ECGDataHighPassed[i];
            }
        } else if (n >= pageSize) {
            array = new float[pageSize];
            for (int i = 0; i < pageSize; i++) {
                array[i] = ECGDataHighPassed[(n - pageSize + i) % cacheSize];
            }
        }
        return array;
    }

    public float[] getDrawArrayL() {
        float array[] = null;
        int n = subscriptHalf;
        if (n > 0 & n < pageSize) {
            array = new float[n + 1];
            for (int i = 0; i <= n; i++) {
                array[i] = ecgDataLowPass[i];
            }
        } else if (n > pageSize) {
            array = new float[pageSize];
            for (int i = 0; i < pageSize; i++) {
                array[i] = ecgDataLowPass[(n - pageSize + i) % cacheSize];
            }
        }
        return array;
    }

    public float[] getDrawArrayH(int channelNo) {
        float array[] = null;
        int n = 0;
        if (channelNo == 0) {
            n = subscript;
        } else {
            n = subscriptHalf;
        }
        transportState();
        if (n > 0 & n < pageSize) {
            array = new float[n + 1];
            for (int i = 0; i <= n; i++) {
                switch (channelNo) {
                    case 0:
                        array[i] = ECGDataHighPassed[i];
                        break;
                    case 1:
                        array[i] = firstChannel[i];
                        break;
                    case 2:
                        array[i] = secondChannel[i];
                        break;
                    case 3:
                        array[i] = thirdChannel[i];
                        break;
                    case 4:
                        array[i] = fourthChannel[i];
                        break;
                    case 5:
                        array[i] = fifthChannel[i];
                        break;
                    case 6:
                        array[i] = sixthChannel[i];
                        break;
                }
            }
        } else if (n >= pageSize) {
            array = new float[pageSize];
            for (int i = 0; i < pageSize; i++) {
                switch (channelNo) {
                    case 0:
                        array[i] = ECGDataHighPassed[(n - pageSize + i) % cacheSize];
                        break;
                    case 1:
                        array[i] = firstChannel[(n - pageSize + i) % cacheSize];
                        break;
                    case 2:
                        array[i] = secondChannel[(n - pageSize + i) % cacheSize];
                        break;
                    case 3:
                        array[i] = thirdChannel[(n - pageSize + i) % cacheSize];
                        break;
                    case 4:
                        array[i] = fourthChannel[(n - pageSize + i) % cacheSize];
                        break;
                    case 5:
                        array[i] = fifthChannel[(n - pageSize + i) % cacheSize];
                        break;
                    case 6:
                        array[i] = sixthChannel[(n - pageSize + i) % cacheSize];
                        break;
                }
            }
        }
        return array;
    }

    private void transportState() {
        if (subscript == oldSub) {
            if (subAdd < 22) {
                subAdd++;
            }
        } else {
            if (answerCallBack != null & !isTransport) {
                answerCallBack.onIsTransport();
            }
            isTransport = true;
            oldSub = subscript;
            subAdd = 0;
        }
        if (subAdd > 20) {
            isTransport = false;
            if (answerCallBack != null) {
                answerCallBack.onBreakTransport();
            }
        }
    }

    public void setAnswerCallBack(AnswerCallback answerCallBack) {
        this.answerCallBack = answerCallBack;
    }

    public void sportFull() {
        pageNumber = nowPageNumber + 30;
    }

    public void stopTest() {
        pageNumber = nowPageNumber + 1;
    }

    public boolean isKnow() {
        return isKnow;
    }

    public void setKnow(boolean know) {
        isKnow = know;
    }


    public int getPower() {
        return power;
    }


    public String getDeviceGatherState() {
        return deviceGatherState;
    }

    public void setDeviceGatherState(String deviceGatherState) {
        this.deviceGatherState = deviceGatherState;
    }

    public String getGatherDate() {
        return gatherDate;
    }

    public int getProgress() {
        if (pageNumber == 0) {
            return 0;
        } else if (byteNumber * 100 / pageNumber / dataPageOne.length > 100) {
            return 100;
        } else {
            return byteNumber * 100 / pageNumber / dataPageOne.length;
        }
    }

    public int getProgressSecond() {
        if (pageNumber == 0) {
            return 0;
        } else if (pageNumber * 10 - byteNumber / 300 < 0) {
            return 0;
        } else {
            return pageNumber * 10 - byteNumber / 300;
        }
    }

    public int getSubscript() {
        return subscript;
    }

    public void setSubscript(int subscript) {
        this.subscript = subscript;
    }
}
